import Utils from "./Utils";
import { wx } from "./Wx";

/*
 * @Description: 
 * @version: 
 * @Author: CoderXZ
 * @Date: 2020-06-09 22:13:28
 * @LastEditors: CoderXZ
 * @LastEditTime: 2020-06-28 22:32:39
 */
const { ccclass, property } = cc._decorator;

@ccclass
export default class Helloworld extends cc.Component {

    @property(cc.Node)
    wxAuthBtn: cc.Node = null;

    @property(cc.Node)
    subViewNode: cc.Node = null;

    @property(cc.Label)
    btnText: cc.Label = null;

    @property(cc.Node)
    btnLastPage: cc.Node = null;

    @property(cc.Node)
    btnNextPage: cc.Node = null;

    @property(cc.Node)
    btnShowRank: cc.Node = null;

    _isAuthed: boolean = false;
    start() {
        // init logic
        this._isAuthed = false;
        this.wxAuthBtn.active = true;
        this.wxAuth();
    }

    getRandomNumBetween(min: number, max: number) {
        return Math.floor(Math.random() * (max - min + 1) + min);
    }

    onClick() {
        console.log("点击上传数据");
        if (!Utils.isWechat()) {
            return;
        }
        let data: any = {};
        data.heroNum = this.getRandomNumBetween(1, 100);
        data.heroTotalLv = this.getRandomNumBetween(1, 1000);
        Utils.updateUserData("friend", data);
    }

    onClickAuth() {
        if (this._isAuthed) {
            console.log("你已经授权过了");
            if (Utils.isWechat()) {
                wx.showToast({
                    title: '你已经授权过了',
                });

                setTimeout(function () {
                    wx.hideToast();
                }, 1000)
            }

        }
    }

    showButtons(isShow: boolean = true) {
        this.btnLastPage.getComponent(cc.Button).interactable = isShow;
        this.btnNextPage.getComponent(cc.Button).interactable = isShow;
        this.btnShowRank.getComponent(cc.Button).interactable = isShow;
    }

    onClickShowSubView() {
        // if (this.subViewNode.active) {
        Utils.sendMessageToSub("clear_content", {});
        // }

        this.subViewNode.active = !this.subViewNode.active;
        if (this.subViewNode.active) {
            this.btnText.string = "隐藏子域";
        } else {
            this.btnText.string = "显示子域";
        }

        this.showButtons(this.subViewNode.active);
    }


    public removeUserCloudData() {
        if (!Utils.isWechat()) {
            return;
        }
        wx.removeUserCloudStorage({
            keyList: ["sentOpenIds"],
            success: function (args) {
                console.log("removeUserCloudStorage 成功.", args);
            },
            fail: function (args) {
                console.log("removeUserCloudStorage 失败.", args);
            }
        })
    }


    public wxAuth() {
        let canvas = cc.find("Canvas");
        let contentSize = this.wxAuthBtn.getContentSize();
        let winSize = cc.winSize;
        let self = this;
        let authBtnPos = self.wxAuthBtn.convertToWorldSpaceAR(cc.v2(0, 0));
        authBtnPos = canvas.convertToNodeSpaceAR(authBtnPos);
        if (!Utils.isWechat()) {
            return;
        }

        let phoneInfo = wx.getSystemInfoSync();
        let scaleX = phoneInfo.screenWidth / winSize.width
        let scaleY = phoneInfo.screenHeight / winSize.height
        let width = contentSize.width * (phoneInfo.screenWidth / winSize.width)
        let height = contentSize.height * (phoneInfo.screenHeight / winSize.height)
        let xDes = phoneInfo.screenWidth / 2 + authBtnPos.x * scaleX - width * self.wxAuthBtn.anchorX;
        let yDes = phoneInfo.screenHeight / 2 - authBtnPos.y * scaleY - height * self.wxAuthBtn.anchorY;
        wx.getSetting({
            success(res) {
                if (!res.authSetting['scope.userInfo']) {
                    let button = wx.createUserInfoButton({
                        type: 'text',
                        text: '授权',
                        style: {
                            left: xDes,
                            top: yDes,
                            width: width,
                            height: height,
                            backgroundColor: '#ff0000',
                            textAlign: 'center',
                            fontSize: 16,
                        }
                    })
                    button.onTap((res) => {
                        console.log("onTap:", res)
                        if (res.userInfo) {
                            wx.getUserInfo({
                                success: function (res) {
                                    console.log("获取用户信息成功,", res);
                                    self._isAuthed = true;
                                    var userInfo = res.userInfo
                                    var nickName = userInfo.nickName
                                    var avatarUrl = userInfo.avatarUrl
                                    var gender = userInfo.gender //性别 0：未知、1：男、2：女
                                    var province = userInfo.province
                                    var city = userInfo.city
                                    var country = userInfo.country
                                },
                                fail: function (res) {
                                    console.log("获取用户信息失败，", res);
                                }
                            })
                        }
                    })
                } else {
                    self._isAuthed = true;
                    console.log("你已经授权过了");
                }
            }
        })
    }
}
